<?php

class Admin_ObjectPhotoProfileController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    	Zend_Layout::startMvc(array("layoutPath"=>APPLICATION_PATH."/layouts/scripts","layout"=>"admin"));
    	$auth = Zend_Auth::getInstance();
    	$infoUser = $auth->getIdentity();
    	$this->view->fullName = $infoUser->ADMINNAME;
    	$this->view->role = $infoUser->ROLE_ID;
    	$this->_model = new Default_Model_DbTable_ObjectPhotoProfile();
    	$this->upload = new Private_Upload();
    }

    public function indexAction()
    {
        // action body
    	$this->view->headTitle("Quản Lý Ảnh Bìa","PREPEND");
    	$this->view->title = "Quản Lý Ảnh Bìa";
        $profiles = $this->_model->getAllObjectPhotoProfile("!=");
        $this->view->allProfile = $profiles;
        $obj = new Default_Model_DbTable_Object();
        $this->view->allObj = $obj->getAllObject();
        if ($this->_request->isPost()){
        	$oid = $this->_getParam("objectId");
        	$stt = $this->_getParam("Stt");
        	$object_lib_photo_id = null;
        	$name = null;
        	$short_description = null;
        	$long_description = null;
        	if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
        		$img = "/homeviet/public/uploaded/images/noavatar.png";
        	}elseif ($this->_getParam("Link")){
        		$img = $this->_getParam("Link");
        	}else{
        		$img = "/homeviet/public/uploaded/images/cardimages/".$_FILES["Img"]["name"];
        		$this->upload->upload("cardimages");
        	}
        	$sDesc = null;
        	$lDesc = null;
        	$olp = null;
        	$this->_model->addObjectPhotoProfile($oid, $object_lib_photo_id, $name, $short_description, $long_description, $img, $stt);
        	$this->_redirect("admin/objectphotoprofile");
        }
    }
    
    public function libphotoAction(){
    	$this->view->headTitle("Quản Lý Ảnh Của Thư Viện","PREPEND");
    	$this->view->title = "Quản Lý Ảnh Của Thư Viện";
    	$profiles = $this->_model->getAllObjectPhotoProfile("=");
    	$this->view->allProfile = $profiles;
    	$olp = new Default_Model_DbTable_ObjectLibPhoto();
    	$this->view->allLib = $olp->getAllObjectLibPhoto();
    	if ($this->_request->isPost()){
    		$oid = null;
    		$stt = $this->_getParam("Stt");
    		$object_lib_photo_id = $this->_getParam("OLibPhoto");
    		$name = null;
    		$short_description = $this->_getParam("Desc");
    		$long_description = $this->_getParam("Content");
    		if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
    			$img = "/homeviet/public/uploaded/images/noavatar.png";
    		}elseif ($this->_getParam("Link")){
    			$img = $this->_getParam("Link");
    		}else{
    			$img = "/homeviet/public/uploaded/images/cardimages/".$_FILES["Img"]["name"];
    			$this->upload->upload("cardimages");
    		}
    		$sDesc = null;
    		$lDesc = null;
    		$olp = null;
    		$this->_model->addObjectPhotoProfile($oid, $object_lib_photo_id, $name, $short_description, $long_description, $img, $stt);
    		$this->_redirect("admin/objectphotoprofile/libphoto");
    	}
    }
    public function editciAction(){
    	$this->view->headTitle("Sửa Ảnh Bìa","PREPEND");
    	$this->view->title = "Sửa Ảnh Bìa";
    	$id = $this->_getParam("id");
    	$cardi = $this->_model->getById($id);
    	$this->view->CardImg = $cardi;#var_dump($cardi);die();
    	$obj = new Default_Model_DbTable_Object();
    	$this->view->allObj = $obj->getAllObject();
    	if ($this->_request->isPost()){
    		$oid = $this->_getParam("objectId");
    		$stt = $this->_getParam("Stt");
    		$object_lib_photo_id = null;
    		$name = null;
    		$short_description = null;
    		$long_description = null;
    		if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
    			$img = "/homeviet/public/uploaded/images/noavatar.png";
    		}elseif ($this->_getParam("Link")){
    			$img = $this->_getParam("Link");
    		}else{
    			$img = "/homeviet/public/uploaded/images/cardimages/".$_FILES["Img"]["name"];
    			$this->upload->upload("cardimages");
    		}
    		$sDesc = null;
    		$lDesc = null;
    		$olp = null;
    		$this->_model->updateObjectPhotoProfile($id, $oid, $object_lib_photo_id, $name, $short_description, $long_description, $img, $stt);
    		$this->_redirect("admin/objectphotoprofile");
    	}
    }
    
    public function editlibpAction(){
    	$this->view->headTitle("Sửa Ảnh Của Thư Viện","PREPEND");
    	$this->view->title = "Sửa Ảnh Của Thư Viện";
    	$id = $this->_getParam("id");
    	$lib = $this->_model->getById($id);
    	$this->view->lib = $lib;
    	$olp = new Default_Model_DbTable_ObjectLibPhoto();
    	$this->view->allLib = $olp->getAllObjectLibPhoto();
    	if ($this->_request->isPost()){
    		$oid = null;
    		$stt = $this->_getParam("Stt");
    		$object_lib_photo_id = $this->_getParam("OLibPhoto");
    		$name = null;
    		$short_description = $this->_getParam("Desc");
    		$long_description = $this->_getParam("Content");
    		if($_FILES["Img"]["name"]=="" and $this->_getParam("Link") == ""){
    			$img = "/homeviet/public/uploaded/images/noavatar.png";
    		}elseif ($this->_getParam("Link")){
    			$img = $this->_getParam("Link");
    		}else{
    			$img = "/homeviet/public/uploaded/images/cardimages/".$_FILES["Img"]["name"];
    			$this->upload->upload("cardimages");
    		}
    		$sDesc = null;
    		$lDesc = null;
    		$olp = null;
    		$this->_model->updateObjectPhotoProfile($id, $oid, $object_lib_photo_id, $name, $short_description, $long_description, $img, $stt);
    		$this->_redirect("admin/objectphotoprofile/libphoto");
    	}
    }
    
    public function del1Action(){
    	$id = $this->_getParam("id");
    	$ci = $this->_model->getById($id);
    	if ($ci["IMG"] != "/homeviet/public/uploaded/images/noavatar.png"){
    		$this->upload->del_img(ROOT_DIR.$ci["IMG"]);
    	}
    	$this->_model->removeObjectPhotoProfile($id);
    	$this->_redirect("admin/objectphotoprofile");
    }
    
    public function del2Action(){
    	$id = $this->_getParam("id");
    	$lib = $this->_model->getById($id);
    	if ($lib["IMG"] != "/homeviet/public/uploaded/images/noavatar.png"){
    		$this->upload->del_img(ROOT_DIR.$lib["IMG"]);
    	}
    	$this->_redirect("admin/objectphotoprofile/libphoto");
    }

    public function activeAction(){
		$id = $this->_getParam("id");
		$opf = $this->_model->activeObjectPhotoProfile($id);
		$this->_redirect("admin/objectphotoprofile");		
    }
    
    public function activelibAction(){
    	$id = $this->_getParam("id");
    	$opf = $this->_model->activeObjectPhotoProfile($id);
    	$this->_redirect("admin/objectphotoprofile/libphoto");
    }

}

